﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SpringMass
{
    public partial class TextBoxSlider : UserControl
    {
        public static DependencyProperty MinValueProperty;
        public static DependencyProperty MaxValueProperty;
        public static DependencyProperty SmallChangeProperty;
        public static DependencyProperty LargeChangeProperty;

        public delegate void SplitterValueChangedHandler(object sender, SplitterEventsArgs sEventsArgs);
        public event SplitterValueChangedHandler OnSplitterValueChanged = null;

        public double MinValue
        {
            get
            {
                return this.NumberSlider.Minimum;
            }
            set
            {
                this.NumberSlider.Minimum = value;
            }
        }

        public double MaxValue
        {
            get
            {
                return this.NumberSlider.Maximum;
            }
            set
            {
                this.NumberSlider.Maximum = value;
            }
        }

        public double SmallChange
        {
            get
            {
                return this.NumberSlider.SmallChange;
            }
            set
            {
                this.NumberSlider.SmallChange = value;
            }
        }

        public double LargeChange
        {
            get
            {
                return this.NumberSlider.LargeChange;
            }
            set
            {
                this.NumberSlider.LargeChange = value;
            }
        }

        static TextBoxSlider()
        {
            MinValueProperty = DependencyProperty.Register( "MinValue", typeof( double ), typeof( TextBoxSlider ), new PropertyMetadata( -10.0, new PropertyChangedCallback( OnMinValueChanged ) ) );
            MaxValueProperty = DependencyProperty.Register( "MaxValue", typeof( double ), typeof( TextBoxSlider ), new PropertyMetadata( 10.0, new PropertyChangedCallback( OnMaxValueChanged ) ) );
            SmallChangeProperty = DependencyProperty.Register( "SmallChange", typeof( double ), typeof( TextBoxSlider ), new PropertyMetadata( 0.5, new PropertyChangedCallback( OnSmallChangeChanged ) ) );
            LargeChangeProperty = DependencyProperty.Register( "LargeChange", typeof( double ), typeof( TextBoxSlider ), new PropertyMetadata( 2.0, new PropertyChangedCallback( OnLargeChangeChanged ) ) );
        }

        public TextBoxSlider()
        {
            InitializeComponent();
        }

        public double Value
        {
            get
            {
                try
                {
                    return double.Parse( this.NumberTextBox.Text.Trim() );
                }
                catch (FormatException)
                {
                    this.Value = this.MinValue;
                    MessageBox.Show( PistonWheel.Properties.Resources.FormatException, PistonWheel.Properties.Resources.ProcessingError, MessageBoxButton.OK, MessageBoxImage.Error );
                    return this.MinValue;
                }
                catch (OverflowException)
                {
                    this.Value = this.MinValue;
                    MessageBox.Show( PistonWheel.Properties.Resources.OverflowException, PistonWheel.Properties.Resources.ProcessingError, MessageBoxButton.OK, MessageBoxImage.Error );
                    return this.MinValue;
                }
                catch (ArgumentNullException)
                {
                    this.Value = this.MinValue;
                    MessageBox.Show( PistonWheel.Properties.Resources.ArgumentNullException, PistonWheel.Properties.Resources.ProcessingError, MessageBoxButton.OK, MessageBoxImage.Error );
                    return this.MinValue;
                }
            }
            set
            {
                if (value >= this.MinValue && value <= this.MaxValue)
                {
                    this.NumberTextBox.Text = String.Format( "{0:F3}", value );
                    this.NumberSlider.Value = value;
                }
            }
        }

        private void NumberSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (this.NumberSlider.Value < this.MinValue || this.NumberSlider.Value > this.MaxValue)
                return;

            this.NumberTextBox.Text = String.Format( "{0:F3}", this.NumberSlider.Value );
            if (this.OnSplitterValueChanged != null)
                OnSplitterValueChanged( sender, new SplitterEventsArgs( this.NumberSlider.Value ) );
        }

        private static void OnMinValueChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            TextBoxSlider tbs = (TextBoxSlider)o;
            tbs.MinValue = (double)e.NewValue;
        }

        private static void OnMaxValueChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            TextBoxSlider tbs = (TextBoxSlider)o;
            tbs.MaxValue = (double)e.NewValue;
        }

        private static void OnSmallChangeChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            TextBoxSlider tbs = (TextBoxSlider)o;
            tbs.SmallChange = (double)e.NewValue;
        }

        private static void OnLargeChangeChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            TextBoxSlider tbs = (TextBoxSlider)o;
            tbs.LargeChange = (double)e.NewValue;
        }
    }
}
